#include<iostream>
using namespace std;
const int N = 1010;
typedef pair<int, int> PII;
#define x first
#define y second
PII q[N * N];
int n, m;
char g[N][N];
int dis[N][N];
int dx[] = { 0,-1,0,1 };
int dy[] = { -1,0,1,0 };
void bfs()
{
	memset(dis, -1, sizeof(dis));
	int hh = 0, tt = -1;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			if (g[i][j] == '1') {
				q[++tt] = { i,j };
				dis[i][j] = 0;
			}
		}
	}
	while (hh <= tt) {
		PII t = q[hh++];
		cout << t.x << " " << t.y << endl;
		for (int i = 0; i < 4; i++) {
			int a = dx[i] + t.x;
			int b = dy[i] + t.y;
			if (a < 0 || a >= n || b < 0 || b >= m)continue;
			if (dis[a][b] != -1)continue;
			dis[a][b] = dis[t.x][t.y] + 1;
			q[++tt] = { a,b };
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> g[i];
	}
	bfs();
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cout << dis[i][j] << " ";
		}
		cout << endl;
    }
	return 0;
}